Gambler's Ruin

In statistics, gambler's ruin is the fact that a persistent gambler with finite wealth, playing a fair game (that is, each bet has expected value of zero to both sides) will eventually and inevitably go broke against an opponent with infinite wealth. The concept was initially stated: A persistent gambler who raises his bet to a fixed fraction of the gambler's bankroll after a win, but does not reduce it after a loss, will eventually and inevitably go broke, even if each bet has a positive expected value.
Below simulates a situation where a game will randomly (50%-50%) increase of deduct a proportion ("Rate") of the money you have, which in the beginning is "Base". Hit plot botton to see how your money changes during the period of you specify in "Period" field.








Final Value (rounded):

See how your money will eventually become in the long run, even though the mathematical expectation of change in money is 0, somewhat meaning you should at least get your base back in the end.

Learn how to play in this playground
1. First don't change any values, use the default values to plot the changes in money. Can you end up with a non-zero final amount?
2. Try a smaller rate (halving recommended), and plot for a few times. Can you get a positive ending this time? How will the gambler think in this case?
3. Use the same small rate as in 2., try a longer period and plot for a few times. What do you end up with now?
4. Can you come up with a strategy that can guarantee a positive profit (ending money-base)? What differs this case from the reality?
Learn how the simulation behind works
This setup mimics a random walk process for a gambling game. Before everything, 3 values are initialized:
1. "base" is initilaized to 10000, which represents gambler's starting money.
2. "odd" is initilaized to 0.5, which determines the change in percentage (positive or negative) that can occur in each step of the random walk. In this case it's increase by 50% or decrease by 50%.
3. "period" is initialized to 365, which represents the number of steps or days in the simulation, in this case, default is 1 year.

First the program enters a loop that iterates times of "period" length. In each iteration, the current value of "base" is appended to a list that records the historical money gambler has. then value is then updated by multiplying it with a random value chosen from either 1-"rate" (go down) or 1+"rate" (go up), which simulates the random walk process.
Then the will display the final money gambler has at the end of the period and plot out the how gambler's money changed using html canvas.
Get Python simulation code
import numpy as np
import matplotlib.pyplot as plt
base=10000
rate=.5
period=365
seq=[]
for i in range(period):
    seq.append(base)
    base*=np.random.choice([1-rate,1+rate])
print(seq[-1])
plt.plot(seq)
Get Matlab simulation code
base = 10000;
rate = 0.5;
period = 365;
seq = zeros(1, period);

for i = 1:period
    seq(i) = base;
    choices = [1-rate, 1+rate];
    base = base * choices(randi([1,2]));
end

fprintf('%.2f\n', seq(end));
plot(seq);